package com.hjh.netty.elect.util;

import java.math.BigDecimal;


/**
 * 数据转换工具类
 * @author pyz
 *
 */
public class DataConvertUtils {

	/**
	 * int转float小数点
	 * @param num int值
	 * @param converNum 乘以的小数点
	 * @return
	 */
	public static float converInt2Float(int num, BigDecimal converNum) {
		BigDecimal bCount = new BigDecimal(num);
		return bCount.multiply(converNum).floatValue();
	}
	

    /**
     * 16进制字符串转为16进制
     * @param hex 16进制的字符串
     * @return
     */
    public static byte[] hexString2Bytes(String hex) {
        if ((hex == null) || (hex.equals(""))){
            return null;
        }
        else if (hex.length()%2 != 0){
            return null;
        }
        else{
            hex = hex.toUpperCase();
            int len = hex.length()/2;
            byte[] b = new byte[len];
            char[] hc = hex.toCharArray();
            for (int i=0; i<len; i++){
                int p=2*i;
                b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p+1]));
            }
            return b;
        }
    }
    
    /**
     * 字符串转byte
     * @param c
     * @return
     */
	private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
	
//	public static void main(String[] args) {
//		byte[] bytes = hexString2Bytes("F9070430120795730FE9F9000000004F102391000102FF009440");
//		System.out.println(bytes);
//	}
}
